package com.xixi.demo.config;

import com.xixi.demo.utils.JWTUtils;
import io.jsonwebtoken.Claims;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;

import java.util.Arrays;
import java.util.List;
import java.util.jar.JarEntry;

@Slf4j
@Component
public class LoginInterceptor implements HandlerInterceptor {

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        String jwtToken = request.getHeader("user_token_header");
        log.info("从header中获取token:{}", jwtToken);

        Claims claims = JWTUtils.verify(jwtToken);
        if(claims != null){
            log.info("令牌认证通过, 放行");
            return true;
        }
        response.setStatus(401);
        return false;
    }
}
